<?php

namespace App\Console\Commands;

use App\Commission;
use App\Http\Controllers\Admin\Business\Repository\ComboOrder;
use App\PayOrder;
use App\Repositories\DistributeRepository;
use Carbon\Carbon;
use Illuminate\Console\Command;

class Member extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'member:handle';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '修补绑定关系 追加订单';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {

        $data=[
          'oGfQ103TyxDdE0NW-ZC7wMjyb5FA'=>4139
        ];

        foreach ($data as $openid=>$leaderId){
            //查询是否绑定关系
            $user=\App\Member::where('openid',$openid)->first();
            if($user&&!$user->pid){
              $distribute=  new DistributeRepository();
              $distribute->distribute($leaderId,$user->id);

              $this->info('绑定关系成功'.$user->id);
              //查询支付订单
             $payOrders= PayOrder::where('openid',$openid)->where('is_pay',1)->get();
             if($payOrders->isNotEmpty()){
                 foreach ($payOrders as $k=>$v){
                     $distribute->account($user->id,DistributeRepository::ACT_COMBO_BUY,['order_id'=>ComboOrder::where('outTradeNo',$v->out_trade_no)->first()->id]);
                     $this->info('out_trade_no'.$v->out_trade_no);
                 }
             }
            }
        }
        foreach (ComboOrder::where('is_refund',1)->where('created_at','>',\Illuminate\Support\Carbon::today())->get() as $k=>$v){
            $orderModel = new \App\Http\Controllers\Admin\Business\Service\ComboOrder();
            $order = $orderModel->getComboOrder($v->id);
            $member=\App\Member::find($v->member_id);
            if ($member->pid) {

                //计算分销价格
                $rateAry = $this->get_real_rate($order);
                $parentMoney = $order->price * $rateAry['rate'] / 100;
                $topMoney = $order->price * $rateAry['top_rate'] / 100;
                //分享记录表
                if($com=Commission::where('desc','订单'.$v->outTradeNo.'退款')->first()){

                    continue;
                }
                $commission=new Commission();
                $commission->to_member=$member->pid;
                $commission->from_member=$member->id;
                $commission->price=$parentMoney;
                $commission->type=9;//退款
                $commission->desc='订单'.$v->outTradeNo.'退款';
                $commission->save();
                $this->info($commission->toJson());
                \App\Member::where('id',$member->pid)->decrement('wallet_all_push_money',$parentMoney);
                \App\Member::where('id',$member->pid)->decrement('wallet_push_money',$parentMoney);
            }
        }
    }
    function get_real_rate($order, $type=1){
        $return = [];
        $order_id = $order->id;
        $sysRate = [
            'rate'  =>  \App\Setting::getConfig('rate','分销设置'),
            'top_rate'  =>  \App\Setting::getConfig('topRate','分销设置')
        ];
        if($order->activity){
            //套餐
            $activity = new \App\Http\Controllers\Admin\Business\Service\ComboActivities();
            $actEntity = $activity->getComboActivities($order->activity->id);

            if($actEntity->rate_type == \App\Http\Controllers\Admin\Business\Service\ComboActivities::RATE_TYPE_SYS){
                $combo = new \App\Http\Controllers\Admin\Business\Service\Combo();
                $entity = $combo->getCombo($order->combo_id);
                if($entity->rate_type==\App\Http\Controllers\Admin\Business\Service\Combo::RATE_TYPE_SYS){
                    //按系统
                    $return  = $sysRate;
                }else{
                    $return = [
                        'rate'    =>  $entity->rate,
                        'top_rate'  =>  $entity->top_rate
                    ];
                }
            }else{
                $return = [
                    'rate'  =>  $actEntity->rate,
                    'top_rate'  =>  $actEntity->top_rate
                ];
            }

        }else{
            //活动
            $combo = new \App\Http\Controllers\Admin\Business\Service\Combo();
            $entity = $combo->getCombo($order->combo_id);
            if($entity->rate_type==\App\Http\Controllers\Admin\Business\Service\Combo::RATE_TYPE_SYS){
                //按系统
                $return  = $sysRate;
            }else{
                $return = [
                    'rate'    =>  $entity->rate,
                    'top_rate'  =>  $entity->top_rate
                ];
            }
        }
        return $return;
    }


}
